<?php

class HmNews_HmNewsController extends Fw_Main
{
	protected $_redirector = null;
	
	public function listAction()
	{
		$_model = $this->_getModel('HmNews');
		$list_news = $_model->getAll();
		$this->view->list_news = $list_news;
		$this->view->rowcount  = count($list_news);
	}
	
	public function lastAction()
	{
		$limit = (int)$this->_request->getParam('limit');
		$_model = $this->_getModel('HmNews');
		$last_news = $_model->getLast('date_register',$limit);
		$this->view->last_news = $last_news;
		$this->view->rowcount = count($last_news);
	}
	
	public function highlightAction()
	{
		$limit = (int)$this->_request->getParam('limit');
		$_model = $this->_getModel('HmNews');
		$highlight_news = $_model->getHighlight('date_register',$limit);
		$this->view->highlight_news = $highlight_news;
		$this->view->rowcount = count($highlight_news);
	}
	
	public function showAction()
	{
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmNews');
		if(FW_MODE == 'client') {
			$_model->assignVisit($id);
		}
		$show_news = $_model->getRow($id);
		$this->view->show_news = $show_news;
	}
	
	public function viewAction()
	{
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmNews');
		if(FW_MODE == 'client') {
			$_model->assignVisit($id);
		}
		$view_news = $_model->getRow($id);
		$this->view->view_news = $view_news;
	}
	
	public function printAction()
	{
		
	}
	
	public function addAction()
	{
		$this->validateUser();
		if($this->_request->isPost()){
			$formData = $this->_request->getPost('add');
			if($formData) {
				$data = array(
							  'title_call'    => $this->_request->getPost('title_call'),
							  'title'         => $this->_request->getPost('title'),
							  'body_call'     => $this->_request->getPost('body_call'),
							  'body'          => $this->_request->getPost('body'),
							  'source'        => $this->_request->getPost('source'),
							  'highlight'     => $this->_request->getPost('highlight'),
							  'date_register' => date('Y-m-d H:i:s'),
							  'views'         => '0',
							  'votes'         => '0',
							  'ranking'       => '0',
							  'active'        => $this->_request->getPost('active')
							  );
				$_model = $this->_getModel('HmNews');
				$add_row = $_model->addRow($data);
				$this->rssAction();
				$this->createDir('HmNews');
				$this->uploadFile('HmNews');
				return $this->_helper->redirector('list');
			}
		}
	}
	
	public function editAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmNews');
		$edit_news = $_model->getRow($id);
		$this->view->edit_news = $edit_news;
		if($this->_request->isPost()){
			$formData = $this->_request->getPost('edit');
			if($formData) {
				$data = array(
							  'title_call'    => $this->_request->getPost('title_call'),
							  'title'         => $this->_request->getPost('title'),
							  'body_call'     => $this->_request->getPost('body_call'),
							  'body'          => $this->_request->getPost('body'),
							  'source'        => $this->_request->getPost('source'),
							  'highlight'     => $this->_request->getPost('highlight'),
							  'active'        => $this->_request->getPost('active')
							  );
				$update_row = $_model->updateRow($id, $data);
				return $this->_helper->redirector('list');
			}
		}
	}
	
	public function deleteAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmNews');
		$delete_user = $_model->deleteRow($id);
		$this->removeDir('HmNews', $id);
		return $this->_helper->redirector('list');
	}
	
	public function categoryAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		
		$this->view->rowid = $id;
		
		$_model = $this->_getModel('HmNewsCategoryDescr');
		$list_category = $_model->getAll();
		$this->view->list_category = $list_category;
		$this->view->count_category = count($list_category);
		
		$_newModel = $this->_getNewModel('HmNewsCategory');
		$link_category = $_newModel->getLink($id);
		$this->view->link_category = $link_category;
		$this->view->count_link = count($link_category);
	}
	
	public function categorySetAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		if($this->_request->isPost()){
			$formData = $this->_request->getPost('add');
			if($formData) {
				$_model = $this->_getModel('HmNewsCategory');
				$data = array('hm_news_id' => $id, 'hm_news_category_descr_id' => $this->_request->getPost('hm_news_category_descr_id'));
				$add_row = $_model->addRow($data);
				
				$this->_redirector = $this->_helper->getHelper('Redirector');
				$this->_redirector->gotoSimple('category','hm-news','hm-news',array('id' => $id));
        		return;
			}
		}
	}
	
	public function categoryUnsetAction()
	{
		
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$news_category = (int)$this->_request->getParam('news_category');
		$_model = $this->_getModel('HmNewsCategory');
		$delete_category = $_model->deleteRow($id, $news_category);
		$this->_redirector = $this->_helper->getHelper('Redirector');
		$this->_redirector->gotoSimple('category','hm-news','hm-news',array('id' => $id));
		return;
	}
	
	public function tagsAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		
		$this->view->rowid = $id;
		
		$_model = $this->_getModel('HmNewsTagsDescr');
		$list_tags = $_model->getAll();
		$this->view->list_tags = $list_tags;
		$this->view->count_tags = count($list_tags);
		
		$_newModel = $this->_getNewModel('HmNewsTags');
		$link_tags = $_newModel->getLink($id);
		$this->view->link_tags = $link_tags;
		$this->view->count_tlink = count($link_tags);
	}
	
	public function tagsSetAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmNewsTags');
		$data = array('hm_news_id' => $id, 'hm_news_tags_descr_id' => $this->_request->getParam('tags_id'));
		$add_row = $_model->addRow($data);
		
		$this->_redirector = $this->_helper->getHelper('Redirector');
		$this->_redirector->gotoSimple('tags','hm-news','hm-news',array('id' => $id));
		return;
	}
	
	public function tagsAddSetAction()
	{
		$this->validateUser();
		if($this->_request->isPost()){
			$formData = $this->_request->getPost('add');
			if($formData) {
				$data = array('description' => $this->_request->getPost('description'));
				$_newModel = $this->_getNewModel('HmNewsTagsDescr');
				$add_row = $_newModel->addRow($data);
			}
			
			$list_row = $_newModel->getLast('id',1);
			$id = (int)$this->_request->getParam('id');
			foreach($list_row as $key) {
				$tags_id = $key['id'];
			}

			$_model = $this->_getModel('HmNewsTags');
			$data = array('hm_news_id' => $id, 'hm_news_tags_descr_id' => $tags_id);
			$add_row = $_model->addRow($data);
			
			$this->_redirector = $this->_helper->getHelper('Redirector');
			$this->_redirector->gotoSimple('tags','hm-news','hm-news',array('id' => $id));
			return;
		}
	}
	
	public function tagsUnsetAction()
	{
		
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$tags = (int)$this->_request->getParam('tags_id');
		$_model = $this->_getModel('HmNewsTags');
		$delete_tags = $_model->deleteRow($id, $tags);
		$this->_redirector = $this->_helper->getHelper('Redirector');
		$this->_redirector->gotoSimple('tags','hm-news','hm-news',array('id' => $id));
		return;
	}
	
	public function rssAction()
	{
		$entries = array();
		
		$_model = $this->_getModel('HmNews');
		$feed = $_model->getLast('id','5');
		
		foreach ($feed as $feed) {
			$entry = array(
						'title'       => $feed['title_call'],
						'description' => $feed['body_call'],
						'pubDate'     => $feed['date_register'],
						'link'        => 'http://localhost/skeleton/hm-news/hm-news/view/id/'.$feed['id']
						);
			array_push($entries, $entry);
		}
		$this->createRss($entries,'HmNews');
	}
}